class Solution(object):
    def divide(self, dividend, divisor):
        if dividend == 0:
            return 0
        if dividend > 0:
            if divisor > 0:
                op = 0
            else:
                op = 1
        else:
            if divisor > 0:
                op = 1
            else:
                op = 0
        dividend = abs(dividend)
        divisor = abs(divisor)
        base_num = divisor
        base_count = 1
        nums = []
        while base_num <= dividend:
            nums.append([base_num, base_count])
            base_num = base_num + base_num
            base_count = base_count + base_count
        ans = 0
        for num, t in nums[::-1]:
            if num <= dividend:
                dividend -= num
                ans += t
        if op == 1:
            ans = - ans
        if - 2 ** 31 <= ans <= 2 ** 31 - 1:
            return ans
        else:
            return 2 ** 31 - 1

